package cn.zhage.aimin.auth1.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.zhage.aimin.auth1.common.Result;
import cn.zhage.aimin.auth1.pojo.Jscode2sessionResult;
import cn.zhage.aimin.auth1.service.WxService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/*
 *
 * @author 渣哥
 */
@RestController
@RequestMapping("/public/wx")
@RequiredArgsConstructor
public class WxController {

    private final WxService wxService;

    @GetMapping("/token")
    public Result<?> token(String code){
        Jscode2sessionResult jscode2sessionResult = wxService.wxLogin(code);
        //数据库存储成功,1代表用户id
        StpUtil.login(1);
        // 获取token信息
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();

        String tokenName = tokenInfo.getTokenName();
        String tokenValue = tokenInfo.getTokenValue();
        if (tokenName == null || tokenValue == null) {
            return Result.error("登录失败");
        }
        return Result.success(tokenInfo);
    }

}
